<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="/js/sweetalert2.all.js"></script>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <link rel="stylesheet" href="css/sweetalert2.css"/>
</head>
<style>
    body {
        background-color: #f2f2f2;
        font-family: Arial, sans-serif;
        font-size: 16px;
    }

    .loginForm {
        padding: 20px;
        margin: 20px;
        border: 1px solid #ccc;
        border-radius: 5px;
        background-color: #fff;
    }

    .databaseTable {
        margin-bottom: 10px;
    }

    .databaseTable label {
        display: inline-block;
        width: 150px;
        font-weight: bold;
    }

    .databaseTable input {
        width: 200px;
        padding: 5px;
        border: 1px solid #ccc;
        border-radius: 3px;
    }

    .loginButton {
        background-color: #4CAF50;
        color: #fff;
        border: none;
        padding: 10px 20px;
        border-radius: 5px;
        cursor: pointer;
    }

    .loginButton:hover {
        background-color: #3e8e41;
    }

</style>
<body>
<form id="databaseform" class="loginForm">
    <div class="databaseTable">
        <label for="address" class="address"><a>数据库地址：</a></label>
        <input type="text" th:field="*{address}" id="address" class="database" placeholder="localhost:3306">
    </div>
    <div class="databaseTable">
        <label for="databasename" class="databasename"><a>数据库名：</a></label>
        <input type="text" th:field="*{databasename}" id="databasename" class="database" placeholder="proc-bidding">
    </div>

    <div class="databaseTable">
        <label for="databasename" class="databasename"><a>数据库用户名：</a></label>
        <input type="text" th:field="*{user}" id="user" class="database" placeholder="user">
    </div>
    <div class="databaseTable">
        <label for="databasename" class="databasename"><a>数据库密码：</a></label>
        <input type="password" th:field="*{password}" id="password" class="database" placeholder="*****">
    </div>
    <input type="submit" class="loginButton" value="提交">
</form>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $(function () {
        const loginForm = $('#databaseform');

        const submitForm = (event) => {
            event.preventDefault();
            const address = $('#address').val();
            const databasename = $('#databasename').val();
            const user = $('#user').val();
            const password = $('#password').val();

            const formData = {address: address, password: password, databasename: databasename, user: user};
            $.ajax({
                type: 'POST',
                url: "/api/changeyaml",
                data: JSON.stringify(formData),
                contentType: 'application/json',
                dataType: 'json',
                success: function (response) {
                    if (response.code == 0) {
                        Swal.fire({
                            icon: 'success',
                            title: 'Success',
                            text: response.msg,
                            footer: '<a href="/generate.html">返回页面</a>'
                        });
                    } else if (response.code = -1) {
                        Swal.fire({
                            icon: 'error',
                            title: '错误',
                            text: response.msg
                        });
                    }
                },
                error: function (xhr, status, error) {
                    // Handle error response
                }
            })
        };

        loginForm.submit(submitForm);

    });
</script>
</body>
</html>